//正则表
export const pattern = {
    exist: /(^s*)|(s*$)/g,
    phoneS: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
    phone: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
    phoneL: /^(?:(?:\+|00)86)?1\d{10}$/,
    idcard: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/,
    email: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
    bank: /^[1-9]\d{9,29}$/,
    carNo: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/,
    symbol: /[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]+/,
}

//非空校验
export const customCheck = params => {
    if (params.item.required && (params.value === '' || params.value === undefined || params.value === null)) {
        return Promise.reject(new Error('请填写'))
    }
    return Promise.resolve()
}
//排除特殊字符
export const special = params => {
    if (!params.value) return Promise.reject(new Error('请填写'))
    let reg = new RegExp(/[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]+/)
    if (reg.test(params.value)) {
        return Promise.reject(new Error('请勿输入特殊符号'))
    } else {
        return Promise.resolve()
    }
}
export const getApply = value => {
    const datas = [
        {
            label: '已完成',
            value: 'COMPLETED',
            color: 'text-green-500',
        },
        {
            label: '未开始',
            value: 'INCOMPLETED',
            color: 'text-gray-400',
        },
        {
            label: '暂停中',
            value: 'PAUSING',
            color: 'text-blue-500',
        },
        {
            label: '进行中',
            value: 'RUNNING',
            color: 'text-yellow-500',
        },
    ]
    for (let index = 0; index < datas.length; index++) {
        const element = datas[index]
        if (value === element.value) {
            return element
        }
    }
}
//数组（对象）去重
export const duplicate = params => {
    const res = new Map()
    return params.arr.filter(a => !res.has(a[params.type]) && res.set(a[params.type], 1))
}
//获取下标
export const getIndex = params => {
    if (params.datas && params.datas.length) {
        for (let index = 0; index < params.datas.length; index++) {
            const element = params.datas[index]
            if (element[params.name] === params.value) {
                return index
            }
        }
        return undefined
    } else {
        return undefined
    }
}
//根据key获取url中参数值
export const getQueryString = params => {
    var href = params.url ? params.url : window.location.href
    var reg = new RegExp('[?&]' + params.field + '=([^&]*)', 'i')
    var string = reg.exec(href)
    return string ? string[1] : null
}

// 根据value值获取名称的集合
export const getOptionsNames = (value, options, key = 'label') => {
    if (value === undefined || value === null) return ''
    value = value.toString().split(',')
    const nameCollection = options
        .filter(item => value.includes(item.value) || value.includes(item.value.toString()))
        .map(item => item[key])
        .toString()
    return nameCollection ? nameCollection : ''
}

